package com.example.myapplication15_2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private StudentDBHelper dbHelper;
    private EditText inputNameEditText;
    private Button addButton;
    private Button searchButton;
    private ListView studentsListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化数据库辅助类
        dbHelper = new StudentDBHelper(this);

        // 初始化UI组件
        inputNameEditText = findViewById(R.id.inputNameEditText);
        addButton = findViewById(R.id.addButton);
        searchButton = findViewById(R.id.searchButton);
        studentsListView = findViewById(R.id.studentsListView);

        // 显示所有学生数据
        displayAllStudents();

        // 设置添加按钮点击事件
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addStudent();
            }
        });

        // 设置查询按钮点击事件
        searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchStudents();
            }
        });
    }

    /**
     * 添加学生
     */
    private void addStudent() {
        String name = inputNameEditText.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "请输入学生姓名", Toast.LENGTH_SHORT).show();
            return;
        }

        long id = dbHelper.addStudent(name);
        if (id != -1) {
            Toast.makeText(this, "成功添加学生：" + name, Toast.LENGTH_SHORT).show();
            inputNameEditText.setText("");  // 清空输入框
            displayAllStudents();  // 刷新列表
        } else {
            Toast.makeText(this, "添加学生失败", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 查询学生
     */
    private void searchStudents() {
        String query = inputNameEditText.getText().toString().trim();
        if (TextUtils.isEmpty(query)) {
            displayAllStudents();  // 如果查询为空，显示所有学生
            return;
        }

        List<String> studentList = dbHelper.searchStudents(query);
        updateListView(studentList);
        
        if (studentList.isEmpty()) {
            Toast.makeText(this, "未找到匹配的学生", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "找到 " + studentList.size() + " 个匹配的学生", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 显示所有学生
     */
    private void displayAllStudents() {
        List<String> studentList = dbHelper.getAllStudents();
        updateListView(studentList);
    }

    /**
     * 更新ListView数据
     */
    private void updateListView(List<String> studentList) {
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, studentList);
        studentsListView.setAdapter(adapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dbHelper != null) {
            dbHelper.close();
        }
    }
}